﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entity;
using System.Data.SqlClient;
using System.Data;

namespace DAL
{
    public class ImageDal:SQLConnection
    {
       
        public List<Image> Image_GetById(string Id)
        {
            List<Image> list = new List<Image>();
            using (SqlCommand dbCmd = new SqlCommand("sp_Image_GetById",Opencon()))
            {
               Image obj = new Image();
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Id", Id));
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(obj.ImageIDataReader(dr));
                    }
                }
                dr.Close();
                obj = null;
            }
            return list;
        }
        
       
        public List<Image> Image_GetByTop(string Top, string Where, string Order)
        {
            List<Image> list = new List<Image>();
            using (SqlCommand dbCmd = new SqlCommand("sp_Image_GetByTop", Opencon()))
            {
               Image obj =new Image();
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Top", Top));
                dbCmd.Parameters.Add(new SqlParameter("@Where", Where));
                dbCmd.Parameters.Add(new SqlParameter("@Order", Order));
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(obj.ImageIDataReader(dr));
                    }
                }
                dr.Close();
                obj = null;
            }
            return list;
        }
        
        
        public List<Image> Image_GetByAll(string Lang)
        {
            List<Image> list = new List<Image>();
            using (SqlCommand dbCmd = new SqlCommand("sp_Image_GetByAll",Opencon()))
            {
               Image obj =new Image();
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Lang", Lang));
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(obj.ImageIDataReader(dr));
                    }
                }
                dr.Close();
                obj = null;
            }
            return list;
        }
        
       
        public bool Image_Insert(Image data)
        {
            using (SqlCommand dbCmd = new SqlCommand("sp_Image_Insert",Opencon()))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Name", data.Name));
                dbCmd.Parameters.Add(new SqlParameter("@Tag", data.Tag));
                dbCmd.Parameters.Add(new SqlParameter("@Title", data.Title));
                dbCmd.Parameters.Add(new SqlParameter("@Description", data.Description));
                dbCmd.Parameters.Add(new SqlParameter("@Keyword", data.Keyword));
                dbCmd.Parameters.Add(new SqlParameter("@Ord", data.Ord));
                dbCmd.Parameters.Add(new SqlParameter("@Active", data.Active));
                dbCmd.Parameters.Add(new SqlParameter("@Lang", data.Lang));
                dbCmd.Parameters.Add(new SqlParameter("@Index", data.Index));
                dbCmd.Parameters.Add(new SqlParameter("@GroupImageId", data.GroupImageId));
                dbCmd.Parameters.Add(new SqlParameter("@File", data.File));
                dbCmd.ExecuteNonQuery();
            }
            //Clear cache
            System.Web.HttpContext.Current.Cache.Remove("Image");
            return true;
        }
        
       
        public bool Image_Update(Image data)
        {
            using (SqlCommand dbCmd = new SqlCommand("sp_Image_Update",Opencon()))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Id", data.Id));
                dbCmd.Parameters.Add(new SqlParameter("@Name", data.Name));
                dbCmd.Parameters.Add(new SqlParameter("@Tag", data.Tag));
                dbCmd.Parameters.Add(new SqlParameter("@Title", data.Title));
                dbCmd.Parameters.Add(new SqlParameter("@Description", data.Description));
                dbCmd.Parameters.Add(new SqlParameter("@Keyword", data.Keyword));
                dbCmd.Parameters.Add(new SqlParameter("@Ord", data.Ord));
                dbCmd.Parameters.Add(new SqlParameter("@Active", data.Active));
                dbCmd.Parameters.Add(new SqlParameter("@Lang", data.Lang));
                dbCmd.Parameters.Add(new SqlParameter("@Index", data.Index));
                dbCmd.Parameters.Add(new SqlParameter("@GroupImageId", data.GroupImageId));
                dbCmd.Parameters.Add(new SqlParameter("@File", data.File));
                dbCmd.ExecuteNonQuery();
            }
            //Clear cache
            System.Web.HttpContext.Current.Cache.Remove("Image");
            return true;
        }
        
        
        public bool Image_Delete(string Id)
        {
            using (SqlCommand dbCmd = new SqlCommand("sp_Image_Delete",Opencon()))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Id", Id));
                dbCmd.ExecuteNonQuery();
            }
            //Clear cache
            System.Web.HttpContext.Current.Cache.Remove("Image");
            return true;
        }
        
    }
}
